package cn.tedu.jsd2207csmall.passport.web;

import cn.tedu.jsd2207csmall.passport.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {

    @ApiModelProperty("状态码")
    private Integer state;

    @ApiModelProperty("操作失败时的描述文本")
    private String message;

    @ApiModelProperty("操作成功是响应的数据")
    private T data;

    public JsonResult() {
    }


    public JsonResult(Integer state, String message) {
        this.state = state;
        this.message = message;
    }

    public static JsonResult<Void> ok() {
        return ok(null);
    }

    public static<T> JsonResult ok(T data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.state = ServiceCode.OK.getValue();
        jsonResult.data=data;
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getServiceCode(),e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode serviceCode, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state = serviceCode.getValue();
        jsonResult.message = message;
        return jsonResult;
    }


}